package org.example.link_list;

public class Reverse_linked_list {
    public static void main(String[] args) {
        //反转链表
        //给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。

        ListNode listNode2 = new ListNode(5);
        listNode2.next = null;
        ListNode listNode3 = new ListNode(4,listNode2);
        ListNode listNode4 = new ListNode(3,listNode3);
        ListNode listNode5 = new ListNode(2,listNode4);
        ListNode head = new ListNode(1,listNode5);
        //反转前打印
        ListNode w = head;
        while (w !=null ){
            System.out.print(w.val);
            w = w.next;
        }
        System.out.println();
        //双指针法
        ListNode p = null;
        ListNode q = null;
        while(head!=null){
            q = head.next;
            head.next = p;
            p = head;
            head = q;
        }
        //反转后打印
        ListNode e = p;
        while (e !=null ){
            System.out.print(e.val);
            e = e.next;
        }

    }
}
